<template>
  <div>
    <div class="flex items-center justify-center my-20">
      <div class="mr-4">
        <img
          alt="程序员的梦工厂"
          class="max-h-14 object-cover"
          src="http://test.codelover.club/static/img/headLogo.d458f61f.png"
        />
      </div>
      <div>
        <p class="text-1xl font-bold" style="color: #707070">程 / 序 / 员 / 的 / 梦 / 工 / 厂</p>
      </div>
    </div>
    <div class="register-container">
      <el-form ref="formRef" :model="form" :rules="rules" class="register-form px-10 py-5">
        <div class="title-container">
          <h2 class="title text-center text-2xl font-bold mb-4">快速注册</h2>
        </div>
        <el-form-item prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
        <el-form-item prop="verificationCode">
          <el-input v-model="form.verificationCode">
            <template #append>
              <el-button type="primary">获取验证码</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            placeholder="请确认密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="userType">
          <el-checkbox-group v-model="form.userType">
            <el-text class="text-lg">请选择用户类型：</el-text>
            <el-checkbox label="大学生" name="type" value="大学生"></el-checkbox>
            <el-checkbox label="中小学生" name="type" value="中小学生"></el-checkbox>
            <el-checkbox label="教务" name="type" value="教务"></el-checkbox>
            <el-checkbox label="家长" name="type" value="家长"></el-checkbox>
            <el-checkbox label="招聘企业" name="type" value="招聘企业"></el-checkbox>
            <el-checkbox label="教师" name="type" value="教师"></el-checkbox>
            <el-checkbox label="社会人员" name="type" value="社会人员"></el-checkbox>
            <el-checkbox label="课程设计人员" name="type" value="课程设计人员"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="" prop="agree">
          <el-checkbox v-model="form.agree"> 同意《在线学院注册协议》和《隐私政策》</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="w-full rounded-2xl" type="primary" @click="submitForm"
            >立即注册
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

const formRef = ref<FormInstance>()
const form = ref({
  email: '',
  verificationCode: '',
  password: '',
  confirmPassword: '',
  userType: [],
  agree: false
})

const rules: FormRules = {
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址格式', trigger: ['blur', 'change'] }
  ],
  verificationCode: [{ required: true, message: '验证码不能为空', trigger: 'blur' }],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度必须在6到20位之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '两次密码不一致！', trigger: 'blur' },
    {
      validator: (_, value: string, callback) => {
        if (value !== form.value.password) {
          callback(new Error('两次输入的密码不一致！'))
        } else {
          callback()
        }
      }
    }
  ],
  userType: [
    { type: 'array', required: true, message: '请至少选择一个用户类型', trigger: 'change' }
  ],
  agree: [
    {
      validator: (_, value, callback) => {
        if (!value) {
          callback(new Error('请阅读并勾选协议'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
}

function submitForm() {
  if (formRef.value == undefined) return
  formRef.value.validate((valid) => {
    if (valid) {
      window.location.href = '#/login'
    } else {
      console.log('提交失败!')
      return false
    }
  })
}
</script>

<style scoped>
.register-container {
  width: 100%;
  max-width: 400px;
  margin: 50px auto;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.register-form .title {
  text-align: center;
  margin-bottom: 20px;
  color: #0e79c0;
}

.el-checkbox {
  margin-right: 5px;
}
</style>
